package com.sky.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.admin.domain.dto.EmployeeDTO;
import com.sky.admin.domain.dto.EmployeeLoginDTO;
import com.sky.admin.domain.dto.EmployeePageQueryDTO;
import com.sky.admin.domain.entity.Employee;
import com.sky.admin.mapper.EmployeeMapper;
import com.sky.admin.service.EmployeeService;
import com.sky.common.constant.MessageConstant;
import com.sky.common.constant.PasswordConstant;
import com.sky.common.constant.StatusConstant;
import com.sky.common.exception.AccountLockedException;
import com.sky.common.exception.AccountNotFoundException;
import com.sky.common.exception.PasswordErrorException;
import com.sky.common.result.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author toki
 */
@RequiredArgsConstructor
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private final EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @return Employee
     */
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //将密码进行md5哈希处理，然后再进行比对 , 数据库中存的也是经过md5处理的密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 新增员工
     */
    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        // 调用BeanUtils工具类copy相同名字的属性
        BeanUtils.copyProperties(employeeDTO, employee);
        // 设置状态,默认正常
        employee.setStatus(StatusConstant.ENABLE);
        // 设置密码,默认为123456,且数据库存储为经过md5处理的
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        // 设置当前记录的创建时间、更新时间为当前系统时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        //  设置创建者和更新者的id
//        employee.setCreateUser(BaseContext.getCurrentId());
//        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.insert(employee);
    }

    /**
     * 员工分页查询
     * @return PageResult
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        // 利用PageHelper分页依赖包进行分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        final long total = page.getTotal();
        final List<Employee> records = page.getResult();
        return new PageResult(total, records);
//        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 员工启用/禁用
     * @param status 状态 0-禁用 1-启用
     * @param id 员工id
     * */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 通过builder模式构建员工对象
        final Employee employee = Employee.builder()
                .id(id)
                .status(status)
                .build();

        employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工信息
     * @param id 员工id
     * */
    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        employee.setPassword("****");
        return employee;
    }

    /**
     * 修改员工信息
     * @param employeeDTO 员工信息
     * */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        final Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.update(employee);
    }

}















